package com.laborer.PTSS.service.impl;

import com.laborer.PTSS.domain.Transaction;
import com.laborer.PTSS.service.PayService;
import com.laborer.PTSS.util.JDBCUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;

public class PayServiceImpl implements PayService {

    private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());

    @Override
    public void addTransaction(String buyerUsername, String sellerUsername, int courseId, int hours, float total) {
        String sql = "insert into tab_transaction(b_uname,s_uname,cid,hour,total) " +
                "values(?,?,?,?,?)";
        //System.out.println(sql);
        template.update(sql, buyerUsername, sellerUsername, courseId, hours, total);
    }

    @Override
    public List<Transaction> getTransaction(String sellerUsername) {
        String sql = "select * from tab_transaction where s_uname = '" + sellerUsername + "'";
        System.out.println(sql);
        return template.query(sql, new BeanPropertyRowMapper<Transaction>(Transaction.class));
    }

    @Override
    public String getCourseName(int cid) {
        String sql = "select title from tab_course where id = ?" + "value(?)";

        return template.queryForObject(sql, new BeanPropertyRowMapper<String>(String.class));
    }
}
